package com.sservice.common;


import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by suweiming on 2017/12/20.
 */

public class CompareObject {
    public static void main(String[] args) {
    }

    public static Map<String, Object> contrastObj(Object obj1, Object obj2) {
        Map<String, Object> res = new HashMap<String, Object>();
        List<String> textList = new ArrayList();
        try {
            Class clazz = obj1.getClass();
            Field[] fields = obj1.getClass().getDeclaredFields();
            for (Field field : fields) {
                if (!field.getName().equals("serialVersionUID")) {
                    if (field.getName().equals("price")) {
                        System.out.println("---");
                    }
                    if (!field.getName().equals("id")) {
                        PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                        Method getMethod = pd.getReadMethod();
                        Object o1 = getMethod.invoke(obj1);
                        Object o2 = getMethod.invoke(obj2);
                        if (o1 != null) {
                            String s1 = o1 == null ? "" : o1.toString();//避免空指针异常
                            String s2 = o2 == null ? "" : o2.toString();//避免空指针异常
                            if (!s1.equals(s2)) {
                                if (field.getName().equals("online")) {
                                    res.put(field.getName(), "由" + (s2.equals("1") ? "在线" : "下线") + "变更为" + (s1.equals("1") ? "上线" : "下线"));
                                } else if(field.getName().equals("status")&&field.toString().toLowerCase().indexOf("offer")>-1){
                                    res.put(field.getName(), "由" + (s2.equals("1") ? "正常报价" : "暂停报价") + "变更为" + (s1.equals("1") ? "正常报价" : "暂停报价"));
                                }else if(field.getName().equals("deliverMethod")&&field.toString().toLowerCase().indexOf("offer")>-1){
                                    res.put(field.getName(), "由" + (s2.equals("1") ? "卖方配送" : "自提") + "变更为" + (s1.equals("1") ? "卖方配送" : "自提"));
                                }else{
                                    res.put(field.getName(), "由" + s2 + "变更为" + s1);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return res;
    }
}
